package com.fc.test.service;

import java.util.List;
import java.util.Arrays;

import com.fc.test.model.auto.ItemCase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.AthingMapper;
import com.fc.test.model.auto.Athing;
import com.fc.test.model.auto.AthingExample;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.StringUtils;

/**
 *  AthingService
 * @Title: AthingService.java 
 * @Package com.fc.test.service 
 * @author fuce_自动生成
 * @email 1345625601@qq.com
 * @date 2020-08-15 18:41:34  
 **/
@Service
public class AthingService implements BaseService<Athing, AthingExample>{
	@Autowired
	private AthingMapper athingMapper;

	public void batchInsert(List<Athing> list) {
		athingMapper.batchInsert(list);
	}


	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Athing> list(Tablepar tablepar,String searchText){
	        AthingExample testExample=new AthingExample();
	        testExample.setOrderByClause("id ASC");
	        if(StrUtil.isNotEmpty(searchText)){
	        	testExample.createCriteria().andItemCaseLike("%"+searchText+"%");
	        }
			if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        }
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<Athing> list= athingMapper.selectByExample(testExample);
	        PageInfo<Athing> pageInfo = new PageInfo<Athing>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			AthingExample example=new AthingExample();
			example.createCriteria().andIdIn(stringB);
			return athingMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public Athing selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return athingMapper.selectByPrimaryKey(id1);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(Athing record) {
		return athingMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(Athing record) {
				
		record.setId(null);
		
				
		return athingMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(Athing record, AthingExample example) {
		
		return athingMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(Athing record, AthingExample example) {
		
		return athingMapper.updateByExample(record, example);
	}

	@Override
	public List<Athing> selectByExample(AthingExample example) {
		
		return athingMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(AthingExample example) {
		
		return athingMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(AthingExample example) {
		
		return athingMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param athing
	 * @return
	 */
	public int checkNameUnique(Athing athing){
		AthingExample example=new AthingExample();
		example.createCriteria().andItemCaseEqualTo(athing.getItemCase());
		List<Athing> list=athingMapper.selectByExample(example);
		return list.size();
	}


	/**
	 * 分页  外部对接
	 * @param pageNum
	 * @param pageSize
	 * @param searchText
	 * @return
	 */
    public PageInfo<Athing> list2(int pageNum, int pageSize, String searchText) {
		AthingExample testExample=new AthingExample();
		testExample.setOrderByClause("id ASC");
		if(StrUtil.isNotEmpty(searchText)){
			testExample.or().andItemCaseLike("%"+searchText+"%");
			testExample.or().andOrgNameLike("%"+searchText+"%");

		}

		PageHelper.startPage(pageNum, pageSize);
		List<Athing> list= athingMapper.selectByExample(testExample);
		PageInfo<Athing> pageInfo = new PageInfo<Athing>(list);
		return  pageInfo;


	}
}
